import {
    toast,
    promptBox
} from 'nc-lightapp-front';

let currentOptSelectedRows = null;
let currentPostData = {};

export default class entryAction {
    constructor(comp) {
        this.comp = comp;
    }

    // 点击确认入职后打开打开弹窗
    openEntryModal = async (selectedRows) => {
        const {props} = this.comp;
        const {dispatch, emp, form, editTable} = props;
        currentOptSelectedRows = selectedRows;
        const ctrtDate = form.getAllFormValue('hi_psndoc_ctrt');
        const emailDate = form.getFormItemsValue('card', 'email').value;
        const materialDate = editTable.getAllData('material');
        const psncode = form.getFormItemsValue('card', 'psncode').value;

        if (!selectedRows && currentOptSelectedRows) {
            selectedRows = currentOptSelectedRows;
        }

        let cardData = {
            ctrt: ctrtDate,
            email: emailDate,
            isCommit: true, //gaile		记得改@@@@@@@@@@@@！！！！！！！！！！
            material: materialDate,
            psncode: psncode,
            pk_org: emp.orgValue.refpk,
            billid: selectedRows[0].values['pk_entryapply'].value
        };
        let postData = {
            pk_org: emp.orgValue.refpk,
            billid: selectedRows[0].values['pk_entryapply'].value,
            isCommit: true,
            ctrt: ctrtDate
        };
        if (emp.ctrtDisp == 'inline') {
            if (form.getFormItemsValue('hi_psndoc_ctrt', 'signdate').value === null ||
                form.getFormItemsValue('hi_psndoc_ctrt', 'pk_majorcorp').value === '' ||
                form.getFormItemsValue('hi_psndoc_ctrt', 'pk_org').value === '' ||
                form.getFormItemsValue('hi_psndoc_ctrt', 'contractnum').value === null ||
                form.getFormItemsValue('hi_psndoc_ctrt', 'termtype').value === null) {
                toast({
                    color: 'danger',
                    content: emp.language['gx6008-000211'] // 劳动合同信息表单必输项为空
                });
                return false
            }
            if (form.getFormItemsValue('hi_psndoc_ctrt', 'ifprop').value) {
                if (form.getFormItemsValue('hi_psndoc_ctrt', 'promonth').value === null ||
                    form.getFormItemsValue('hi_psndoc_ctrt', 'probegindate').value === null ||
                    form.getFormItemsValue('hi_psndoc_ctrt', 'probenddate').value === null) {
                    toast({
                        color: 'danger',
                        content: emp.language['gx6008-000211'] // 劳动合同信息表单必输项为空
                    });
                    return false
                }
            }
            if (form.getFormItemsValue('hi_psndoc_ctrt', 'termtype').value === 'CM01') {
                if (form.getFormItemsValue('hi_psndoc_ctrt', 'termmonth').value === null ||
                    form.getFormItemsValue('hi_psndoc_ctrt', 'begindate').value === null ||
                    form.getFormItemsValue('hi_psndoc_ctrt', 'enddate').value === null) {
                    toast({
                        color: 'danger',
                        content: emp.language['gx6008-000211'] // 劳动合同信息表单必输项为空
                    });
                    return false
                }
            } else if (form.getFormItemsValue('hi_psndoc_ctrt', 'termtype').value === 'CM02') {
                if (form.getFormItemsValue('hi_psndoc_ctrt', 'begindate').value === null) {
                    toast({
                        color: 'danger',
                        content: emp.language['gx6008-000211'] // 劳动合同信息表单必输项为空
                    });
                    return false
                }
            } else if (form.getFormItemsValue('hi_psndoc_ctrt', 'termtype').value === 'CM03') {
                if (form.getFormItemsValue('hi_psndoc_ctrt', 'begindate').value === null) {
                    toast({
                        color: 'danger',
                        content: emp.language['gx6008-000211'] // 劳动合同信息表单必输项为空
                    });
                    return false
                }
            }
        } else {//合同表单为空，不传ctrt
            cardData = {
                email: emailDate,
                isCommit: true, //gaile		记得改@@@@@@@@@@@@！！！！！！！！！！
                material: materialDate,
                psncode: psncode,
                pk_org: emp.orgValue.refpk,
                billid: selectedRows[0].values['pk_entryapply'].value
            };
            postData = {
                pk_org: emp.orgValue.refpk,
                billid: selectedRows[0].values['pk_entryapply'].value,
                isCommit: true
            };


        }
        //currentCardData = cardData;
        if (!form.getFormItemsValue('card', 'psncode').value) {
            toast({
                color: 'danger',
                content: emp.language['gx6008-000212'] //'入职手续办理表单必输项为空' 
            });
            return false
        }

        try {
            let res = await dispatch({
                type: 'emp/handEntryCheck',
                payload: {
                    postData: postData
                }
            });

            if (!res.success) return;
            if (!res.data || !res.data.confirmMsg || res.data.confirmMsg.length < 1) {
                promptBox({
                    color: 'info',
                    title: emp.language['gx6008-000018'],/* 国际化处理： 提示*/
                    content: emp.language['gx6008-000213'],/* 国际化处理： 是否确认入职*/
                    beSureBtnClick: () => {
                        this.handEntry(cardData, selectedRows);
                    }
                });
                return
            }
            promptBox({
                color: 'info',
                title: emp.language['gx6008-000018'],/* 国际化处理： 提示*/
                content: res.data.confirmMsg[0],
                beSureBtnClick: () => {
                    if (res.data.confirmMsg.length > 1) {
                        promptBox({
                            color: 'info',
                            title: emp.language['gx6008-000018'],/* 国际化处理： 提示*/
                            content: res.data.confirmMsg[1],
                            beSureBtnClick: () => {
                                this.handEntry(cardData, selectedRows);
                            }
                        });
                        return;
                    }
                    this.handEntry(cardData, selectedRows);
                }
            });
        } catch (e) {
            console.error(e)
        }
    }

    // 点击确认入职
    handEntry = async (postData, selectedRows) => {
        const {props, action} = this.comp;
        const {emp, editTable, dispatch, form, button, meta} = props;
        try {
            let res = await dispatch({
                type: 'emp/handEntry',
                payload: {
                    postData: postData
                }
            });

            if (!res.success) return;
            let template3 = meta.getMeta();
            template3 = action.formAct.initMetaBrowse(template3);
            meta.setMeta(template3);
            try {
                let oneBillData = await action.formAct.getOneBillData(selectedRows);
                form.setAllFormValue({
                    card: oneBillData.data.card.card
                });
                editTable.setTableData('material', oneBillData.data.material.material, false);
                form.setAllFormValue({
                    hi_psndoc_ctrt: oneBillData.data.hi_psndoc_ctrt.hi_psndoc_ctrt
                });
                form.setFormItemsDisabled('card', {
                    psncode: !oneBillData.data.isPsncodEditable
                });
                form.setFormItemsDisabled('hi_psndoc_ctrt', {
                    contractnum: !oneBillData.data.isCtrtEditable
                })
            } catch (e) {
                console.error(e)
            }
            dispatch({
                type: 'emp/update',
                payload: {
                    handEntryModalVisible: false,
                    addPageStatus: 'edit',//我觉得应该改成edit，browse下边有个保存按钮
                    ctrtBtnDisp: 'none',
                    ctrtOpenBtnDisp: 'none',
                    isUnentryEdit: false
                }
            });
            form.setFormStatus('card', 'browse');
            editTable.setStatus('material', 'browse');
            form.setFormStatus('hi_psndoc_ctrt', 'browse');
            button.setButtonVisible({
                save: false,
                handentry: false
            });
            toast({
                color: 'success',
                content: emp.language['gx6008-000052'] // 操作成功
            });
        } catch (e) {
            console.error(e)
        }
    }


    // 关闭确认入职弹窗
    closeHandEntryModal = () => {
        const {props} = this.comp;
        const {dispatch} = props;

        currentOptSelectedRows = null;

        dispatch({
            type: 'emp/update',
            payload: {
                handEntryModalVisible: false
            }
        });
    }

    // 点击保存入职信息
    saveOnes = async (selectedRows) => {
        const {props} = this.comp;
        const {emp, editTable, dispatch, form} = props;
        const ctrtDate = form.getAllFormValue('hi_psndoc_ctrt');
        const emailDate = form.getFormItemsValue('card', 'email').value;
        const materialDate = editTable.getAllData('material');
        const psncode = form.getFormItemsValue('card', 'psncode').value;
        let cardData = {
            ctrt: ctrtDate,
            email: emailDate,
            isCommit: false,
            material: materialDate,
            psncode: psncode,
            pk_org: emp.orgValue.refpk,
            billid: selectedRows[0].values['pk_entryapply'].value
        };
        let postData = {
            pk_org: emp.orgValue.refpk,
            billid: selectedRows[0].values['pk_entryapply'].value,
            isCommit: false,
            ctrt: ctrtDate
        };
        //设置表单必输性？？？
        if (emp.ctrtDisp == 'inline') {
            if (!form.isCheckNow(['card', 'hi_psndoc_ctrt'])) {
                return
            }
        } else {
            if (!form.isCheckNow('card')) {
                return
            }
            cardData = {
                email: emailDate,
                isCommit: false,
                material: materialDate,
                psncode: psncode,
                pk_org: emp.orgValue.refpk,
                billid: selectedRows[0].values['pk_entryapply'].value
            };
            postData = {
                pk_org: emp.orgValue.refpk,
                billid: selectedRows[0].values['pk_entryapply'].value,
                isCommit: false
            };

        }

        dispatch({
            type: 'emp/handEntryCheck',
            payload: {
                postData: postData
            }
        }).then( (res) => {
            if (!res.success) return;
            if (res.data && res.data.confirmMsg && res.data.confirmMsg.length) {
                if (res.data.confirmMsg.length === 1) {
                    promptBox({
                        color: 'info',
                        title: emp.language['gx6008-000018'],/* 国际化处理： 提示*/
                        content: res.data.confirmMsg[0],
                        beSureBtnClick: () => {
                            this.handSave(cardData, selectedRows);
                        }
                    });
                } else {
                    promptBox({
                        color: 'info',
                        title: emp.language['gx6008-000018'],/* 国际化处理： 提示*/
                        content: res.data.confirmMsg[0],
                        beSureBtnClick: () => {
                            promptBox({
                                color: 'info',
                                title: emp.language['gx6008-000018'],/* 国际化处理： 提示*/
                                content: res.data.confirmMsg[1],
                                beSureBtnClick: () => {
                                    this.handSave(cardData, selectedRows);
                                }
                            });
                        }
                    });
                }
                return;
            }
            promptBox({
                color: 'info',
                title: emp.language['gx6008-000018'],/* 国际化处理： 提示*/
                content: emp.language['gx6008-000266'],/* 国际化处理： 是否确认保存*/
                beSureBtnClick: () => {
                    this.handSave(cardData, selectedRows);
                }
            });    
        });
    }


    // 确认保存
    handSave = async (postData, selectedRows) => {
        const {props, action} = this.comp;
        const {emp, editTable, dispatch, form, button, meta} = props;
        try {
            let res = await dispatch({
                type: 'emp/handEntry',
                payload: {
                    postData: postData
                }
            });
            if (res.success) {
                let template2 = meta.getMeta();
                template2 = action.formAct.initMetaBrowse(template2);
                meta.setMeta(template2);
                form.setFormStatus('card', 'browse');
                editTable.setStatus('material', 'browse');
                form.setFormStatus('hi_psndoc_ctrt', 'browse');
                try {
                    let oneBillData = await action.formAct.getOneBillData(selectedRows);
                    form.setAllFormValue({
                        card: oneBillData.data.card.card
                    });
                    editTable.setTableData('material', oneBillData.data.material.material, false);
                    form.setAllFormValue({
                        hi_psndoc_ctrt: oneBillData.data.hi_psndoc_ctrt.hi_psndoc_ctrt
                    });
                    form.setFormItemsDisabled('card', {
                        psncode: !oneBillData.data.isPsncodEditable
                    });
                    form.setFormItemsDisabled('hi_psndoc_ctrt', {
                        contractnum: !oneBillData.data.isCtrtEditable
                    })
                } catch (e) {
                    console.error(e)
                }
                dispatch({
                    type: 'emp/update',
                    payload: {
                        addPageStatus: 'browse',
                        ctrtBtnDisp: 'none',
                        ctrtOpenBtnDisp: 'none',
                        isUnentryEdit: false
                    }
                });
                // form.setFormStatus('card', 'browse');
                // editTable.setStatus('material', 'browse');
                // form.setFormStatus('hi_psndoc_ctrt', 'browse');
                button.setButtonVisible({
                    save: false
                });
                toast({
                    color: 'success',
                    content: emp.language['gx6008-000030'] // "保存成功"
                });
            }
        } catch (e) {
        }
    }

    //编辑按钮
    editOnes = (selectedRows) => {
        const {props, action} = this.comp;
        const {dispatch} = props;
        dispatch({
            type: 'emp/update',
            payload: {
                isUnentryEdit: true
            }
        });
        action.formAct.toEditPage(selectedRows);
    }
}